package com.baizhi.service.impl;

import com.baizhi.entity.Category;
import com.baizhi.entity.Menu;
import com.baizhi.mapper.CategoryMapper;
import com.baizhi.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author haiyu
 * @since 2021-09-16
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> selectTree() {
        List<Category> tree = baseMapper.selectList(null);
        List<Category> collect = tree.stream().filter((cate) -> {
            return cate.getCategoryLevel() == 1;
        }).peek(
                (cate) -> {
                    cate.setChildren(getChildren(cate.getCategoryId(), tree));
                }
        ).collect(Collectors.toList());
        return  collect;
    }
    //根据传来的id值,这个id是父菜单的id,来找该id下面的所有子菜单的集合
    public List<Category> getChildren(Long parentId, List<Category> tree){
        return  tree.stream().filter((cate) -> {
            return cate.getParentId() == parentId;
        }).peek((cate)->{cate.setChildren(getChildren(cate.getCategoryId(),tree));}).collect(Collectors.toList());

    }
}
